package ldh.im.fxgui.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import ldh.im.ImConfig;
import ldh.im.fxbase.data.pojo.ImUser;
import ldh.im.fxbase.util.MovableHelper;
import ldh.im.fxbase.util.StageUtil;
import ldh.im.fxgui.util.UiUtil;
import ldh.im.fxgui.util.UserHeadImageUtil;
import ldh.im.util.ConfigUtil;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * 配置管理
 */
public class SettingController implements Initializable {

    @FXML private StageControlController stagePaneController;
    @FXML private Pane movablePane;
    @FXML private Pane settingContentPane;
    @FXML private Pane settingLeftPane;

    // 账号设置
    @FXML private Pane accountSettingPane;
    @FXML private ImageView headImageView;
    @FXML private TextArea userDescTextArea;
    @FXML private TextField nameTextField;
    @FXML private TextField userIdTextField;

    // 文件管理
    @FXML private Pane fileSettingPane;
    @FXML private TextField fileDirTextField;
    @FXML private CheckBox autoDownloadCheckBox;

    // 快捷键设置
    @FXML private Pane shortCutkeySettingPane;
    @FXML private ChoiceBox sendMsgChoiceBox;
    @FXML private Button captureBtn;
    @FXML private Button openImBtn;

    // IM关于
    @FXML private Pane aboutPane;
    @FXML private TextArea contentTextArea;

    public void setStage(Stage stage) {
        stagePaneController.setStage(stage);
    }

    public Pane getMovablePane() {
        return movablePane;
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
//        stagePaneController.hideMax();
        addLeftPaneActive();
        ImUser imUser = UiUtil.getLoginImUser();
        headImageView.setImage(UserHeadImageUtil.getHeadImage(imUser.getHeadImg()));
        userDescTextArea.setText(imUser.getMotto());
        nameTextField.setText(imUser.getUserName());
        userIdTextField.setText(imUser.getUserId());

        MovableHelper.movable(movablePane);

        visible(accountSettingPane);

        sendMsgChoiceBox.getItems().addAll("Enter", "Ctrl + Enter");
        sendMsgChoiceBox.getSelectionModel().select("Ctr + Enter");

        fileDirTextField.setText(ConfigUtil.getRoot());
    }

    @FXML
    private void accountSettingAct(ActionEvent actionEvent) {
        visible(accountSettingPane);
    }

    @FXML
    private void fileSettingAct(ActionEvent actionEvent) {
        visible(fileSettingPane);
    }

    @FXML
    private void shortCutkeySettingAct(ActionEvent actionEvent) {
        visible(shortCutkeySettingPane);
    }

    @FXML
    private void aboutAct(ActionEvent actionEvent) {
        visible(aboutPane);
    }

    private void addLeftPaneActive() {
        settingLeftPane.getChildren().forEach(n->{
           n.addEventFilter(MouseEvent.MOUSE_CLICKED, e->{
               clearActiveStyle();
               n.getStyleClass().add("active");
           });
        });
    }

    private void clearActiveStyle() {
        settingLeftPane.getChildren().forEach(n->n.getStyleClass().remove("active"));
    }

    public void exitLoginAct(ActionEvent actionEvent) {
        stagePaneController.close(null);
        StageUtil.getStage().setScene(null);
        StageUtil.getStage().close();
        System.exit(0);
    }

    private void visible(Node pane) {
        settingContentPane.getChildren().forEach(node->node.setVisible(false));
        pane.setVisible(true);
    }
}
